package com.springboot.controller;

import com.springboot.base.baseConrtoller;
import com.springboot.model.user;
import com.springboot.service.userService;
import com.springboot.utill.userproperty;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.repository.query.Param;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
//@SpringBootApplication 注解等价于以默认属性使用 @Configuration ， @EnableAutoConfiguration 和 @ComponentScan
@RequestMapping(value = "/user")
public class userController extends baseConrtoller {
    @Autowired
    private userService userservice;
    //    @Value("${age}")
//    private Integer age;
//    @Value("${cupSize}")
//    private String cupSize;
//    @Value("${content}")
//    private String content;
    @Autowired
    private userproperty userproper;

    @RequestMapping(value = {"/test", "/hi"}, method = RequestMethod.GET)
    public String test(@RequestParam(value = "id", required = false, defaultValue = "0") Integer id) {
        //  return cupSize+age+content;
        //  return userproper.getAdress();
        return "id" + id;
    }

    @RequestMapping(value = "/valid", method = RequestMethod.POST)
    public user valid(@Valid user userdata, BindingResult bindingresult){
        if(bindingresult.hasErrors()){
            System.out.println(bindingresult.getFieldError().getDefaultMessage());
            return null;
        }
        return userservice.saveuser(userdata);
    }

    //       Optional<user> usermap= null;
//       try {
//    user userdata=new user();
//    userdata.setUsername("朱立如");
//    userdata.setAge(12);
//    userservice.save(userdata);
//     long id=1;
//    usermap= userservice.find(id);
//     String name="lisan";
//     List<user> list= userservice.finaByname(name);
//       } catch (Exception e) {
//           e.printStackTrace();
//       } finally {
//             return  this.getResultObjectData("101","sds",usermap );
//       }
    @GetMapping(value = "/userlist")
    public List<user> userList() {
        System.out.println("第二");
        return userservice.userList();
    }

    /**
     * @param username
     * @param age
     * @return
     */
    @RequestMapping(value = "/adduser")
    public user adduser(@RequestParam(value = "username", required = false) String username,
                        @RequestParam(value = "age", required = false) Integer age) {
        user userdata = new user();
        userdata.setAge(age);
        userdata.setUsername(username);
        return userservice.saveuser(userdata);

    }

    /**
     *
     */
    @GetMapping(value = "/findbyid/{id}")
    public Optional<user> findbyid(@PathVariable("id") Long id) {
        return userservice.findbyid(id);
    }

    /**
     * @param id
     */
    @DeleteMapping(value = "/deleteuser/{id}")
    public void deleteuser(@PathVariable("id") Long id) {
        userservice.deletuser(id);
    }

    /**
     * @param id
     * @return
     */
    @PutMapping(value = "/updateuser/{id}")
    public user updateuser(@PathVariable("id") Long id) {
        user userdata = new user();
        userdata.setId(id);
        userdata.setUsername("朱立如");
        userdata.setAge(26);
        return userservice.updateuser(userdata);
    }

    @GetMapping(value = "/findbyage/{age}")
    public List<user> findbyage(@PathVariable("age") Integer age) {
        List<user> arry = new ArrayList<>();
        arry = userservice.findbyage(age);

        if (arry != null && arry.size() > 0) {
            return arry;
        } else {
            return null;
        }

    }

       @PostMapping(value = "/inserttwo")
        public void inserttwo() {
        userservice.inserttwo();
      }
 @GetMapping(value = "/exception/{id}")
   public void exception(@PathVariable("id") Long id) throws Exception {
        userservice.exception(id);
 }
}
